package ultweb.service.Validator;

import java.util.regex.Pattern;

import org.springframework.validation.Validator;
import org.springframework.validation.Errors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ultweb.service.Registrazione;

public class registrazioneValidator implements Validator {
    private int DEFAULT_MIN_PERCENTAGE = 0;
    private int DEFAULT_MAX_PERCENTAGE = 50;
    private int minPrezzo = DEFAULT_MIN_PERCENTAGE;
    private int maxPrezzo = DEFAULT_MAX_PERCENTAGE;
    private static final Pattern PATTERN_nome = Pattern.compile("[\\w\\d\\s]{1,50}");
    private static final Pattern PATTERN_prezzo = Pattern.compile("[\\d]{1,6}");
    
    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    public boolean supports(Class clazz) {
         if (Registrazione.class.equals(clazz)){;
         	return true;
         }else {return false;}
    }

    public void validate(Object obj, Errors errors) {
        
    	Registrazione pi =  (Registrazione) obj;
        
        if (pi == null) {
            errors.rejectValue("prezzo", "error.not-specified", null, "Value required.");
        }
        else {
            logger.info("Validating with " + pi + ": " + pi.getPrezzo());
            if (pi.getPrezzo() > maxPrezzo) {
                errors.rejectValue("prezzo", "error.too-high",
                    new Object[] {new Integer(pi.getPrezzo())}, "Value too high.");
            }
            if (pi.getPrezzo() <= minPrezzo) {
                errors.rejectValue("prezzo", "error.too-low",
                    new Object[] {new Integer(minPrezzo)}, "Value too low.");
            }
            
            if(pi.getQuantita()<1){
            	errors.rejectValue("quantitaer", "quantity.too-low",
                        new Object[] {new Integer(pi.getQuantita())}, "Value too low.");
            }
        }
    }

    public void setMinPrezzo(int i) {
    	minPrezzo = i;
    }

    public int getMinPrezzo() {
        return minPrezzo;
    }

    public void setMaxPrezzo(int i) {
        maxPrezzo = i;
    }

    public int getMaxPrezzo() {
        return maxPrezzo;
    }

}